package com.dmap.analysispoint.service.impl;

import com.dmap.analysispoint.bo.ActivePhoneBo;
import com.dmap.analysispoint.bo.AddAndActivePhoneBo;
import com.dmap.analysispoint.controller.background.param.CountParam;
import com.dmap.analysispoint.dao.BurialPointAddAndActivePhoneMapper;
import com.dmap.analysispoint.po.BurialPointAddAndActivePhoneExample;
import com.dmap.analysispoint.service.BurialPointAddAndActivePhoneService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* @author:zhouhao
 * \* Date:25/06/2018
 * \* Description:
 * \*
 */
@Service
public class BurialPointAddAndActivePhoneServiceImpl implements BurialPointAddAndActivePhoneService {

    @Resource
    private BurialPointAddAndActivePhoneMapper burialPointAddAndActivePhoneMapper;


    @Override
    public Integer sumAddPhoneByExample(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointAddAndActivePhoneExample example = this.getParamExample(param);
        return burialPointAddAndActivePhoneMapper.sumAddPhoneByExample(example);
    }

    @Override
    public ActivePhoneBo selectActivePhoneByExample(CountParam param) {
        if (param == null) {
            return null;
        }
        BurialPointAddAndActivePhoneExample example = this.getParamExample(param);
        return burialPointAddAndActivePhoneMapper.selectActivePhoneByExample(example);
    }

    @Override
    public List<AddAndActivePhoneBo> selectAddAndActivePhoneList(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddAndActivePhoneExample example = this.getParamExample(param);
        return burialPointAddAndActivePhoneMapper.selectAddAndActivePhoneListByExample(example);
    }

    @Override
    public Long selectMaxCreateTimeByProjectId(CountParam param) {
        if (param == null) {
            return 0L;
        }
        BurialPointAddAndActivePhoneExample example = this.getParamExample(param);
        return burialPointAddAndActivePhoneMapper.selectMaxCreateTimeByProjectId(example);
    }

    /**
     *BurialPointAddAndActivePhone表查询时对查询条件进行处理
     */
    private BurialPointAddAndActivePhoneExample getParamExample(CountParam param) {
        BurialPointAddAndActivePhoneExample example = new BurialPointAddAndActivePhoneExample();
        BurialPointAddAndActivePhoneExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getStartTime() != null && param.getEndTime() == null){
            criteria.andCreateTimeEqualTo(param.getStartTime());
        }else {
            if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
                criteria.andCreateTimeBetween(param.getStartTime(), param.getEndTime());
            }
        }
        return example;
    }
}
